package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.CustomerCategoryDao;
import com.zhiyou100.crm.util.CustomerCategory;
import com.zhiyou100.crm.util.DBUtil1;
import com.zhiyou100.crm.util.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CustomerCategoryDaoImpl implements CustomerCategoryDao {
    ResultSet rs=null;
    DBUtil1 dbUtil1=new DBUtil1();
    @Override
    public List<CustomerCategory> queryAll() {
        List<CustomerCategory> customerCategories= new ArrayList<>();
        String sql="select * from customer_category";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()) {
                int customer_category_id = rs.getInt("customer_category_id");
                String customer_category_name = rs.getString("customer_category_name");
                String customer_category_desc = rs.getString("customer_category_desc");
                String remark = rs.getString("remark");
                Timestamp create_time = rs.getTimestamp("create_time");
                Timestamp update_time = rs.getTimestamp("update_time");
                int status = rs.getInt("status");
                int creater = rs.getInt("creater");
                int updater = rs.getInt("updater");
                CustomerCategory customerCategory=new CustomerCategory(customer_category_id,customer_category_name,customer_category_desc,status,remark,create_time,creater,update_time,updater);
                customerCategories.add(customerCategory);
            }
            return customerCategories;
        }catch (SQLException e){
            e.printStackTrace();
            return  null;
        }finally {
            dbUtil1.close();
        }
    }

    @Override
    public int update(CustomerCategory category) {
        String sql="update customer_category set customer_category_name=?,customer_category_desc=?,update_time=?,updater=?  where customer_category_id=? ";
        Object[] obj={category.getCustomer_category_name(),category.getCustomer_category_desc(),category.getUpdate_time(),category.getUpdate(),category.getCustomer_category_id()};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int delete(int id) {
        String sql="delete from customer_category where customer_category_id=?";
        Object[] obj={id};
        return dbUtil1.update(sql,obj);
    }

}
